// pages/message/message.js
const app = getApp()
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mymessage:[],
    callMe:[],
    loading:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //先把小红点去除
    db.collection("message").where({
      openid:this._openid
    }).update({
      data:{
        message:false
      }
    })
    this._index=0
    //第一次拿数据
    this.getData()
  },
  getData(){
    this.setData({
      loading:true,
      vanTop: app.globalData.vanTop,
    })
    let mymessage=db.collection("comments").orderBy('time', 'desc').skip(this._index*6).limit(6).where({
      _openid:app.globalData.openid
    }).get().then((res)=>{
        if(res.data.length!==0){
          this.setData({
            mymessage:this.data.mymessage.concat(res.data)
          })
         }
      }
    )
    let callMe=db.collection("commentBack").orderBy('time', 'desc').skip(this._index*6).limit(6).where({
      receiveid:app.globalData.openid
    }).get().then((res)=>{
      this.setData({
        callMe:this.data.callMe.concat(res.data)
      })
    })
    //两边的数据都拿完以后再加页和到底
    Promise.all([mymessage,callMe]).then(()=>{
      this._index++
      this.setData({
        loading:false
      })
    })
  },
  // 下拉刷新
  onPullDownRefresh(){
    this.setData({
      loading: true,
      mymessage:[],
      callMe:[]
    })
    this._index=0
    this.getData()
    wx.stopPullDownRefresh()
  },
  //到底加载更多数据
  onReachBottom(){
    this.getData()
  },
  // 跳转
  go(e){
    const underCollection=e.currentTarget.dataset.item.underCollection
    const underid=e.currentTarget.dataset.item.underid
    //对于noticeMore.js文件被两个调用时的尴尬
    const underTitle=e.currentTarget.dataset.item.underTitle==="通知公告"?"index":"training"
    wx.navigateTo({
      url: `/pages/${underCollection}/${underCollection}?_id=${underid}&comment=true&title=${underTitle}`,
    })
  },
  delete(e){
    const id=e.currentTarget.dataset.doc
    wx.showModal({
      title: '删除确认',
      content: '删除该条数据后及评论',
      success: (res)=>{
        wx.showLoading({
          title: '删除中',
        })
        if (res.confirm) {
          db.collection("comments").doc(id).get({
            success:(res)=>{
              let index=0
              const length=res.data.comment.length+res.data.goodNum.length
              let allArr=res.data.comment.concat(res.data.goodNum)
              allArr.map(item=>{
                db.collection("commentBack").doc(item.connectid).remove({
                  success:(r)=>{
                    index++
                    if(index===length){
                      db.collection("comments").doc(id).remove({
                        success:(res)=>{
                          wx.hideLoading()
                          wx.startPullDownRefresh()
                        }
                      })
                    }
                  }
                })
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})